﻿using System;
using System.Collections.Specialized;
using System.Reflection;
using VitCode.Castle.DataAccess;
using VitCode.ObjectResolutionHelper;

namespace VitCode.Castle.Entities
{
    /// <summary>
    /// Core业务对象的基类
    /// </summary>
    public abstract class BaseEntity
    {
        public static string ID = "Id";
        protected User m_CreateUser;


        protected Guid m_Id;
        protected string m_ImportNum;
        protected bool m_IsLogicDelete;
        protected string m_KeyWord;
        protected DateTime m_LastEditDate = DateTime.Now;
        protected User m_LastEditUser;

        protected string m_Memo;

        /// <summary>
        /// 数据主键
        /// </summary>
        [OrMemo("Id")]
        public virtual Guid Id
        {
            get { return m_Id; }
            set { m_Id = value; }
        }


        /// <summary>
        /// 記錄創建的系統用戶ID
        /// </summary>
        [OrMemo("创建用户")]
        public virtual User CreateUser
        {
            get { return m_CreateUser; }
            set { m_CreateUser = value; }
        }

        /// <summary>
        /// 最後一次記錄修改的系統用戶Id
        /// </summary>
        [OrMemo("最后一次操作用户")]
        public virtual User LastEditUser
        {
            get { return m_LastEditUser; }
            set { m_LastEditUser = value; }
        }

        /// <summary>
        /// 最後一次記錄修改的時間
        /// </summary>
        [OrMemo("最后一次操作时间")]
        public virtual DateTime LastEditDate
        {
            get { return m_LastEditDate; }
            set { m_LastEditDate = value; }
        }

        /// <summary>
        /// 記錄的導入批次號(在新增記錄時生成,同一批次導入記錄,產生同一個批次號)
        /// </summary>
        [OrMemo("导入批次号")]
        public virtual string ImportNum
        {
            get { return m_ImportNum; }
            set { m_ImportNum = value; }
        }

        /// <summary>
        /// 邏輯刪除的標記
        /// </summary>
        [OrMemo("逻辑删除标记")]
        public virtual bool IsLogicDelete
        {
            get { return m_IsLogicDelete; }
            set { m_IsLogicDelete = value; }
        }

        /// <summary>
        /// 關鍵字(以","分格)
        /// </summary>
        [OrMemo("关键字")]
        public virtual string KeyWord
        {
            get { return m_KeyWord; }
            set { m_KeyWord = value; }
        }

        /// <summary>
        /// 备注
        /// </summary>
        [OrMemo("备注")]
        public virtual string Memo
        {
            get { return m_Memo; }
            set { m_Memo = value; }
        }

        [OrMemo("当前对应的历史记录")]
        public virtual Guid CurrHisId { get; set; }

        /// <summary>
        /// 获取对象不需要进行差异比较的属性
        /// </summary>
        /// <returns></returns>
        public virtual StringCollection GetNotCompareHisProNames()
        {
            return new StringCollection {ID, "LastEditDate", "LastEditUser", "ImportNum", "CreateUser"};
        }

        /// <summary>
        /// 获取对象的系统预留的属性,此属性一般不显示到UI层
        /// </summary>
        /// <returns></returns>
        public virtual StringCollection GetSysProNames()
        {
            return new StringCollection
                       {ID, "LastEditDate", "LastEditUser", "ImportNum", "CreateUser", "OrderId", "IsLogicDelete"};
        }

        /// <summary>
        /// 获取对象的关键属性(业务主键)
        /// </summary>
        /// <returns></returns>
        public virtual StringCollection GetKeyProNames()
        {
            return new StringCollection {"Name"};
        }

        /// <summary>
        /// 获取对象的唯一约束属性
        /// </summary>
        /// <returns></returns>
        public virtual StringCollection GetUniqueComposeProNames()
        {
            PropertyInfo p = GetType().GetProperty("Name");
            if (p != null)
                return new StringCollection {p.Name};
            p = GetType().GetProperty("Code");
            if (p != null)
                return new StringCollection {p.Name};
            else
                throw new ArgumentException("找不到业务唯一主键,默认[Name]或[Code]");
        }

        /// <summary>
        /// 获取生成的导入批次号
        /// </summary>
        /// <returns></returns>
        public static string GetImportNum()
        {
            return DateTime.Now.ToString("yyyyMMddhhmmssffff");
        }

        /// <summary>
        /// 设置系统属性
        /// </summary>
        public virtual void SetSysExPro()
        {
            m_LastEditDate = DateTime.Now;
            m_LastEditUser = GenericContext.CurrUser;
            m_CreateUser = GenericContext.CurrUser;
        }

        /// <summary>
        /// 在对象Save方法执行后,自动执行的方法
        /// </summary>
        public virtual void SetSysExProAfterSave()
        {
            PropertyInfo pPro = GetType().GetProperty("Parent");
            PropertyInfo orderIdPro = GetType().GetProperty("OrderId");
            if (orderIdPro != null && orderIdPro.PropertyType == typeof (string) && pPro != null &&
                pPro.PropertyType == GetType())
            {
                object pProVal = pPro.GetValue(this, null);
                if (pProVal != null)
                {
                    string pOrderId = orderIdPro.GetValue(pProVal, null).ToString();
                    orderIdPro.SetValue(this, pOrderId + "." + m_Id, null);
                }
                else
                {
                    orderIdPro.SetValue(this, m_Id.ToString(), null);
                }
            }
        }
    }
}